#include "fileactions.h"

#include <XmlSerializer.h>


#include <QDebug>
#include <QFile>
#include <QMetaType>


Campaign *FileActions::create(const QString& campaignName, QObject* parent)
{
    qRegisterMetaType<Campaign*>("Campaign");

    Campaign* campaign = new Campaign(campaignName, parent);
    campaign->initialize();
    return campaign;
}

Campaign *FileActions::open(const QString &fileName, QObject* parent)
{
    if (fileName.isEmpty())
    {
        return 0;
    }

    QFile file(fileName);
    if (!file.open(QFile::ReadOnly))
    {
        return 0;
    }


    XmlSerializer serializer;

    qRegisterMetaType<Campaign*>("Campaign");
    Campaign* campaign = qobject_cast<Campaign*>(serializer.deserialize(&file));
    if (!campaign)
    {
        return 0;
    }

    campaign->setParent(parent);
    campaign->initialize();
    return campaign;
}

bool FileActions::save(Campaign *campaign, const QString &fileName)
{
    if (fileName.isEmpty())
    {
        return false;
    }

    QFile file(fileName);
    if (!file.open(QFile::WriteOnly))
    {
        return false;
    }

    XmlSerializer serializer;
    serializer.serialize(campaign, &file);

    return true;
}

